<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>您正在做的业务是:</el-breadcrumb-item>
      <el-breadcrumb-item>生产设计</el-breadcrumb-item>
      <el-breadcrumb-item>产品档案管理</el-breadcrumb-item>
      <el-breadcrumb-item>产品档案登记</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider content-position="left">主信息</el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="ruleForm.productName"></el-input>
      </el-form-item>
      <el-form-item label="制造商" prop="factoryName">
        <el-input v-model="ruleForm.factoryName"></el-input>
      </el-form-item>
      <el-form-item label="产品简称" prop="productNick">
        <el-input v-model="ruleForm.productNick"></el-input>
      </el-form-item>
      <el-form-item label="选择一级种类名称:">
        <el-select v-model="pid,value" placeholder="选择一级种类" @change="changeLevel2(value)" ref="selectCh">
          <el-option value='' label="请选择"></el-option>
          <el-option @click.native="getPid(item.pid)"
                     v-for="item in fileKind"
                     :key="item.id"
                     :label="item.kindName"
                     :value="item.id">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="选择二级种类名称:" v-if="fileKind2!=null&&fileKind2!=''">
        <el-select v-model="value2" placeholder="选择二级种类" @change="changeLevel3(value2)" ref="selectCh">
          <el-option value='' label="请选择"></el-option>
          <el-option
              v-for="item in fileKind2"
              :key="item.id"
              :label="item.kindName"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="选择三级种类名称:" v-if="fileKind3!=null&&fileKind3!=''">
        <el-select v-model="value3" placeholder="选择三级种类" @change="changeLevel4(value3)" ref="selectCh">
          <el-option value='' label="请选择"></el-option>
          <el-option
              v-for="item in fileKind3"
              :key="item.id"
              :label="item.kindName"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用途类别" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择用途类别">
          <el-option label="商品" value="Y001-1"></el-option>
          <el-option label="物料" value="Y001-2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="档次级别" prop="productClass">
        <el-select v-model="ruleForm.productClass" placeholder="请选择档次级别">
          <el-option label="高档" value="D001-1"></el-option>
          <el-option label="中档" value="D001-2"></el-option>
          <el-option label="低档" value="D001-3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="计量单位" prop="personalUnit">
        <el-input v-model="ruleForm.personalUnit"></el-input>
      </el-form-item>
      <el-form-item label="计量值" prop="personalValue">
        <el-input v-model="ruleForm.personalValue"></el-input>
      </el-form-item>
      <el-form-item label="市场单价" prop="listPrice">
        <el-input v-model="ruleForm.listPrice"></el-input>
      </el-form-item>
      <el-form-item label="计划成本单价" prop="costPrice">
        <el-input v-model="ruleForm.costPrice"></el-input>
      </el-form-item>

      <el-divider content-position="left">辅助信息</el-divider>
      <el-form-item label="保修期" prop="warranty">
        <el-input v-model="ruleForm.warranty"></el-input>
      </el-form-item>
      <el-form-item label="替代品名称" prop="twinName">
        <el-input v-model="ruleForm.twinName"></el-input>
      </el-form-item>
      <el-form-item label="替代品编号" prop="twinId">
        <el-input v-model="ruleForm.twinId"></el-input>
      </el-form-item>
      <el-form-item label="生命周期(年)" prop="lifecycle">
        <el-input v-model="ruleForm.lifecycle"></el-input>
      </el-form-item>
      <el-form-item label="单位" prop="amountUnit">
        <el-input v-model="ruleForm.amountUnit"></el-input>
      </el-form-item>
      <el-form-item label="产品描述" prop="productDescribe">
        <el-input type="textarea" v-model="ruleForm.productDescribe"></el-input>
      </el-form-item>
      <el-form-item label="产品经理" prop="responsiblePerson">
        <el-input v-model="ruleForm.responsiblePerson"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
import request from "../request.js"

export default {
  data() {
    return {
      fileKind2: [],
      fileKind3: [],
      fileKind: [],
      value: '', value2: '',
      value3: '',
      value4: '',
      pid: '',
      ruleForm: {
        productName: '',
        factoryName: '',
        productNick: '',
        type: '',
        productClass: '',
        firstKindId: '',
        firstKindName: '',
        secondKindId: '',
        secondKindName: '',
        thirdKindId: '',
        thirdKindName: '',
        warranty: '',
        twinName: '',
        twinId: '',
        lifecycle: '',
        amountUnit: '',
        productDescribe: '',
        responsiblePerson: '',
        personalValue: '',
        personalUnit: '',
        listPrice: '',
        costPrice: '',
      },
      rules: {
        productName: [
          {required: true, message: '请输入产品名称', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择用途类别', trigger: 'change'}
        ],
        productClass: [
          {required: true, message: '请选择档次级别', trigger: 'change'}
        ],
        firstKindId: [
          {required: true, message: '请选择一级分类', trigger: 'change'}
        ],
        secondKindId: [
          {required: true, message: '请选择二级分类', trigger: 'change'}
        ],
        thirdKindId: [
          {required: true, message: '请选择三级分类', trigger: 'change'}
        ],

        listPrice: [
          {required: true, message: '请输入市场单价', trigger: 'blur'},
          {min: 3, max: 5, message: '市场单价不能为空', trigger: 'blur'}
        ],
        costPrice: [
          {required: true, message: '请输入计划成本单价', trigger: 'blur'},
          {min: 3, max: 5, message: '计划成本单价不能为空', trigger: 'blur'}
        ],
      }
    };
  },
  methods: {

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.post("http://localhost:8150/file/addFile", this.ruleForm).then(resp => {
            if (resp.data.flag==false) {
              this.$message({
                showClose: true,
                message: data.data.message,
                type: resp.data.flag == true ? "success" : "error"
              })
              return;
            }
            if (resp.data.flag) {
              this.ruleForm = {};
              this.value = '';
              this.value2 = '';
              this.value3 = '';
            }
            this.$message({
              showClose: true,
              message: resp.data.message,
              type: resp.data.flag == true ? 'success' : 'error'
            });
          }).catch(e => {
            alert("错误提示:" + e);
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    queryFineConfig() {
      axios.get("http://localhost:8150/config-file-kind/displayType").then(data => {
        this.fileKind = data.data.data;
      });
    }, changeLevel2(id) {
      this.ruleForm.firstKindId = id;
      for (let i = 0; i < this.fileKind.length; i++) {
        if (id == this.fileKind[i].id) {
          this.ruleForm.firstKindName = this.fileKind[i].kindName;
        } else {
          this.ruleForm.firstKindName = '';
          this.ruleForm.firstKindId = '';
          this.value = '';
        }
      }
      console.log("id:" + id)
      let a = 0;
      for (let i = 0; i < this.fileKind.length; i++) {
        for (let j = 0; j < this.fileKind[i].list.length; j++) {
          if (id === this.fileKind[i].list[j].pid) {
            this.fileKind2[a] = this.fileKind[i].list[j];
            a++;
          } else {
            this.fileKind2 = [];
            this.fileKind3 = [];
            this.ruleForm.secondKindId = '';
            this.ruleForm.secondKindName = '';
            this.ruleForm.thirdKindId = '';
            this.ruleForm.thirdKindName = '';
            this.value2 = '';
            this.value3 = '';
          }
        }
      }
    }, changeLevel3(id) {
      //  console.log(id)
      for (let i = 0; i < this.fileKind2.length; i++) {
        if (id == this.fileKind2[i].id) {
          this.ruleForm.secondKindName = this.fileKind2[i].kindName;
        } else {
          this.ruleForm.secondKindName = '';
          this.ruleForm.secondKindId = '';
          this.value3 = '';
        }
      }
      let a = 0;
      this.ruleForm.secondKindId = id;
      for (let i = 0; i < this.fileKind2.length; i++) {
        for (let j = 0; j < this.fileKind2[i].list.length; j++) {
          if (id == this.fileKind2[i].list[j].pid) {
            this.fileKind3[a] = this.fileKind2[i].list[j];
            a++;
          } else {
            this.fileKind3 = []
            this.ruleForm.secondKindId = '';
            this.ruleForm.secondKindName = '';
            this.value3 = '';
          }
        }
      }
    }, changeLevel4(id) {
      this.ruleForm.thirdKindId = id;
      for (let i = 0; i < this.fileKind3.length; i++) {
        if (id == this.fileKind3[i].id) {
          this.ruleForm.thirdKindName = this.fileKind3[i].kindName;
        } else {
          this.ruleForm.thirdKindId = '';
        }
      }
    },
    getPid(id) {
      this.pid = id;
    },
  }, created() {
    this.queryFineConfig();
  }
}
</script>

<style scoped>

</style>